﻿package njClass.UI{
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import njAct.*;
	
	public class PlusCombo extends Sprite {
		public var currentID:int;
		public var limitNum:int=100;
		private var Value:*;
		private var radio:Sprite;
		private var btns:Array=new Array();
		public var Dates:Array=new Array();
		private var Txts:Array=new Array();
		private var BGColors:Array=new Array();
		private var chosed:TextField;
		private var ban:Sprite=new Sprite();
		private var Width:int;
		private var buttonTxt:Array=new Array();
		
		public function PlusCombo(w:int,dates:Array,txts:Array,defaultID:int=0,bgColors:Array=null) {
			Width=w;
			Txts=txts;
			Dates=dates;
			BGColors=bgColors;
			currentID=defaultID;
			Value=Dates[currentID];
			limitNum=txts.length;
			radio=new Sprite();
			chosed=new TextField();
			chosed.selectable=false;
			chosed.defaultTextFormat=new TextFormat("宋体",12);
			chosed.width=w-5;
			chosed.autoSize=TextFieldAutoSize.CENTER;
			chosed.height=18;
			chosed.text=txts[currentID];
			radio.addChild(chosed);
			var kick:SimpleButton=new SimpleButton;
			kick.upState=doDraw(0x666666);
			kick.downState=kick.overState=doDraw(0xFF9900);
			kick.hitTestState=doDrawRect(w);
			kick.x=w;
			kick.y=4;
			radio.addChild(kick);
			createBan(w);
			radio.addEventListener(MouseEvent.CLICK,showBan);
			addChild(radio);
		}
		
		/*public function SetdataArray(arr1:Array,arr2:Array):void{
			Dates.length=0;
			Dates=arr1;
			Txts.length=0;
			Txts=arr2;
			if(BGColors!=null){
				BGColors.length=Dates.length;
			}
			currentID=0;
		}*/
		
		public function get value():* {
			return Value;
		}
		
		public function set value(num:*):void {
			Value=num;
			currentID=Dates.indexOf(num);
			chosed.text=Txts[currentID];
		}
		
		private function createBan(w:Number):void {
			var mask_mc:Sprite=new Sprite();
			mask_mc.addChild(doDrawLine(110,16*Txts.length+26,0x000000));
			mask_mc.y=18;
			mask_mc.x=-2;
			ban.mask=mask_mc;
			radio.addChild(mask_mc);
			ban.addChild(doDrawLine(100+6,16*Txts.length+6,0x000000));
			for (var i:int=0; i<Txts.length; i++) {
				var btn:SimpleButton=new SimpleButton();
				var TempUp:Sprite=new Sprite();
				var txt:TextField=new TextField();
				buttonTxt.push(txt);
				txt.selectable=false;
				txt.defaultTextFormat=new TextFormat("宋体",12);
				txt.width=w-5;
				txt.height=18;
				txt.text=Txts[i];
				txt.autoSize=TextFieldAutoSize.CENTER;
				txt.y=0;
				njAct.myText.TextAlpha(txt);
				if(BGColors!=null){
					TempUp.addChild(doDrawRect(100,true,BGColors[i]));
				}else{
					TempUp.addChild(doDrawRect(100,true,0xCCCCCC));
				}
				TempUp.addChild(txt);
				btn.upState=btn.downState=TempUp;
				var TempOver:Sprite=new Sprite();
				var txtOver:TextField=new TextField();
				txtOver.selectable=false;
				txtOver.defaultTextFormat=new TextFormat("宋体",12);
				txtOver.width=w-5;
				txtOver.height=18;
				txtOver.text=Txts[i];
				txtOver.autoSize=TextFieldAutoSize.CENTER;
				txtOver.y=0;
				njAct.myText.TextAlpha(txtOver);
				//TempOver.alpha=0.4;
				TempOver.addChild(doDrawRect(100,true,0xFF9900));
				TempOver.addChild(txtOver);
				btn.overState=TempOver;
				btn.hitTestState=doDrawRect(100,true);
				btn.y=16*i;
				btns.push(btn);
				ban.addChild(btn);
			}
			btns[currentID].upState=btns[currentID].overState;
			ban.alpha=0;
		}
		
		private function showBan(e:MouseEvent):void {
			radio.removeEventListener(MouseEvent.CLICK,showBan);
			radio.stage.addEventListener(MouseEvent.MOUSE_DOWN,disShowBan);
			ban.addEventListener(MouseEvent.MOUSE_MOVE,showBanAct);
			njAct.myMove.ChangeSpeedMove(ban,"y",(2-16*currentID),20,8,false);
			//ban.y=20;
			alphaBanPlus();
			radio.addChild(ban);
			//trace(radio.parent.parent);
			radio.parent.parent.addChild(radio.parent);
		}
		
		private function showBanAct(e:MouseEvent):void {
			if (currentID!=btns.indexOf(e.target)&&btns.indexOf(e.target)!=-1) {
				for (var i:int=0; i<btns.length; i++) {
					btns[i].upState=btns[i].downState;
				}
				e.target.upState=e.target.overState;
				currentID=btns.indexOf(e.target);
			}
		}
		
		private function disShowBan(e:MouseEvent):void {
			ban.removeEventListener(MouseEvent.MOUSE_MOVE,showBanAct);
			radio.stage.removeEventListener(MouseEvent.MOUSE_DOWN,disShowBan);
			njAct.myMove.ChangeSpeedMove(ban,"y",ban.y,(2-16*currentID),8,false);
			alphaBanSub();
			chosed.text=Txts[currentID];
			Value=Dates[currentID];
			dispatchEvent(new Event("SliderChange"));
		}
		
		private function alphaBanPlus():void {
			ban.alpha=0;
			ban.addEventListener(Event.ENTER_FRAME,alphaAct);
			
			function alphaAct(e:Event):void {
				if (Math.round(ban.alpha*3)==3) {
					ban.removeEventListener(Event.ENTER_FRAME,alphaAct);
					return;
				}
				ban.alpha=Math.round(ban.alpha*10+1)/10;
			}
		}
		
		private function alphaBanSub():void {
			ban.alpha=1;
			ban.addEventListener(Event.ENTER_FRAME,alphaAct);
			
			function alphaAct(e:Event):void {
				if (Math.round(ban.alpha*3)==0) {
					radio.addEventListener(MouseEvent.CLICK,showBan);
					ban.removeEventListener(Event.ENTER_FRAME,alphaAct);
					try {
						radio.removeChild(ban);
					} catch (e:ArgumentError) {
						trace(e);
					}
					return;
				}
				ban.alpha=Math.round(ban.alpha*10-1)/10;
			}
		}
		
		private function doDraw(bgColor:uint=0x000000):Shape {
			var child:Shape=new Shape;
			child.graphics.beginFill(bgColor);
			child.graphics.moveTo(0,0);
			child.graphics.lineTo(12,0);
			child.graphics.lineTo(6,6 * Math.sqrt(3));
			child.graphics.lineTo(0,0);
			child.graphics.endFill();
			return child;
		}
		
		private function doDrawLine(w:Number,h:Number,bgColor:uint=0x000000):Shape {
			var child:Shape=new Shape;
			child.graphics.beginFill(0xFFFFFF);
			child.graphics.lineStyle(2, bgColor);
			child.graphics.drawRoundRect(-3,-3,w-5,h-1,5);
			child.graphics.endFill();
			return child;
		}
		
		private function doDrawRect(w:int,Forbtns:Boolean=false,bgColor:uint=0x000000,h:Number=15):Shape {
			var child:Shape=new Shape;
			child.graphics.beginFill(bgColor);
			if (Forbtns) {
				child.graphics.drawRoundRect(0,0,w-5,h,5);
			} else {
				child.graphics.drawRect(-5-w,-5,22+w,22);
			}
			child.graphics.endFill();
			return child;
		}
	}
}